﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LED_Tester
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
        }

        #region 变量声明

        private double gammaSample;
        private double gammaRange;
        private int cycleSpace;
        private int measSpace;
        private bool isAutoAdjust;
        private int intTime;
        private int sta;
        private int bcw;
        private string luxPortName;
        private double distance;
        private int sourceMode;
        private string sourcePortName;
        private double voltage;
        private double current;
        private int preheatTime;

        private bool isMeas = false;

        public double GammaSample
        {
            get { return gammaSample; }
            set { gammaSample = value; }
        }
        public double GammaRange
        {
            get { return gammaRange; }
            set { gammaRange = value; }
        }
        public int CycleSpace
        {
            get { return cycleSpace; }
            set { cycleSpace = value; }
        }
        public int MeasSpace
        {
            get { return measSpace; }
            set { measSpace = value; }
        }
        public bool IsAutoAdjust
        {
            get { return isAutoAdjust; }
            set { isAutoAdjust = value; }
        }
        public int IntTime
        {
            get { return intTime; }
            set { intTime = value; }
        }
        public int STA
        {
            get { return sta; }
            set { sta = value; }
        }
        public int BCW
        {
            get { return bcw; }
            set { bcw = value; }
        }
        public string LuxPortName
        {
            get { return luxPortName; }
            set { luxPortName = value; }
        }
        public double Distance
        {
            get { return distance; }
            set { distance = value; }
        }
        public int SourceMode
        {
            get { return sourceMode; }
            set { sourceMode = value; }
        }
        public string SourcePortName
        {
            get { return sourcePortName; }
            set { sourcePortName = value; }
        }
        public double Voltage
        {
            get { return voltage; }
            set { voltage = value; }
        }
        public double Current
        {
            get { return current; }
            set { current = value; }
        }
        public int PreheatTime
        {
            get { return preheatTime; }
            set { preheatTime = value; }
        }

        public bool IsMeas
        {
            get { return isMeas; }
            set { isMeas = value; }
        }

        #endregion

        #region 功能函数

        private static void TxtDataVerifyWithOnePoint(KeyPressEventArgs e, TextBox txt)
        {
            int pointCount = 0;
            bool isPointExist = false;
            if (char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == (char)Keys.Back)
            {
                e.Handled = false;
                if (txt.Text.Length == 0)
                {
                    if (e.KeyChar == '.')
                        e.Handled = true;
                }

                for (int i = 0; i < txt.Text.Length; i++)
                {
                    if (txt.Text.Substring(i, 1) == ".")
                        pointCount++;
                    if (pointCount > 0)
                        isPointExist = true;
                }

                if (isPointExist && e.KeyChar == '.')
                    e.Handled = true;
            }

            else { e.Handled = true; }
        }

        #endregion

        #region 窗体事件

        private void frmSetting_Load(object sender, EventArgs e)
        {
            cmbGammaSample.SelectedItem = gammaSample.ToString("#0.0");
            cmbGammaRange.SelectedItem = gammaRange.ToString();
            nudCycleSpace.Value = cycleSpace;
            nudMeasSpace.Value = measSpace;

            if (isAutoAdjust)
            {
                cbxAutoAdjust.Checked = true;
                nudIntTime.Enabled = false;
            }
            else
            {
                cbxAutoAdjust.Checked = false;
                nudIntTime.Enabled = true;
            }
            nudIntTime.Value = intTime;
            nudSTA.Value = sta;
            nudBCW.Value = bcw;

            cmbLuxBoardPortName.SelectedItem = luxPortName;

            textBox1.Text = distance.ToString();

            if (sourceMode == 0)
            {
                cmbSouceMode.SelectedIndex = 0;
            }
            else if (sourceMode == 1)
            {
                cmbSouceMode.SelectedIndex = 1;
            }
            else if (sourceMode == 2)
            {
                cmbSouceMode.SelectedIndex = 2;
            }
            else if (sourceMode == 3)
            {
                cmbSouceMode.SelectedIndex = 3;
            }

            cmbSourcePortName.SelectedItem = sourcePortName;

            txtVoltage.Text = voltage.ToString("#0.0000");
            txtCurrent.Text = current.ToString("#0.0000");

            nudPreheatTime.Value = preheatTime;

            if (IsMeas)
            {
                cmbGammaSample.Enabled = false;
                cmbGammaRange.Enabled = false;
            }
        }

        private void frmSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            gammaSample = double.Parse(cmbGammaSample.SelectedItem.ToString());
            gammaRange = double.Parse(cmbGammaRange.SelectedItem.ToString());
            cycleSpace = (int)nudCycleSpace.Value;
            measSpace = (int)nudMeasSpace.Value;

            if (cbxAutoAdjust.Checked)
            {
                isAutoAdjust = true;
            }
            else
            {
                isAutoAdjust = false;
            }
            intTime = (int)nudIntTime.Value;
            sta = (int)nudSTA.Value;
            bcw = (int)nudBCW.Value;

            luxPortName = cmbLuxBoardPortName.SelectedItem.ToString();
            distance = double.Parse(textBox1.Text);

            sourceMode = cmbSouceMode.SelectedIndex;
            sourcePortName = cmbSourcePortName.SelectedItem.ToString();
            voltage = double.Parse(txtVoltage.Text);
            current = double.Parse(txtCurrent.Text);
            preheatTime = (int)nudPreheatTime.Value;
        }

        #endregion

        #region 其它事件

        private void button1_Click(object sender, EventArgs e)
        {
            if (cmbLuxBoardPortName.SelectedItem.ToString() == cmbSourcePortName.SelectedItem.ToString())
            {
                MessageBox.Show("请勿重复使用同一个端口！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void cbxAutoAdjust_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxAutoAdjust.Checked)
            {
                nudIntTime.Enabled = false;
            }
            else
            {
                nudIntTime.Enabled = true;
            }
        }

        private void txtVoltage_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtVoltage);
        }

        private void txtCurrent_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, txtCurrent);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TxtDataVerifyWithOnePoint(e, textBox1);
        }

        #endregion

       
    }
}
